package org.musicsweet.service;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.musicsweet.constant.StringConstant;
import org.musicsweet.entity.MusicAlbum;
import org.musicsweet.entity.SysUser;
import org.musicsweet.mapper.MusicAlbumMapper;
import org.musicsweet.result.ResultData;
import org.musicsweet.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 专辑Service
 */
@Slf4j
@Service
public class MusicAlbumService {

    @Resource
    private MusicAlbumMapper musicAlbumMapper;

    @Autowired
    private SysUserService sysUserService;


    /**
     * 根据id查询专辑
     *
     * @param musicAlbum
     * @return
     */
    public ResultData<MusicAlbum> getMusicAlbumById(MusicAlbum musicAlbum) {
        log.info("根据id查询专辑:"+ JSON.toJSONString(musicAlbum));
        if(StringUtils.isEmpty(musicAlbum.getId())){
            return ResultData.createFail("id不能为空");
        }
        musicAlbum.setDel(StringConstant.DEL_NO);
        MusicAlbum data = musicAlbumMapper.getMusicAlbumById(musicAlbum);
        return ResultData.successData("查询成功",data);
    }

    /**
     * 多条件查询专辑
     *
     * @param musicAlbum
     * @return
     */
    public ResultData<List<MusicAlbum>> getMusicAlbumList(MusicAlbum musicAlbum) {
        log.info("多条件查询专辑:"+ JSON.toJSONString(musicAlbum));
        musicAlbum.setDel(StringConstant.DEL_NO);
        List<MusicAlbum> data = musicAlbumMapper.getMusicAlbumList(musicAlbum);
        return ResultData.successData("查询成功",data);
    }


    /**
     * 添加专辑
     * @param musicAlbum
     * @param token
     * @return
     */
    @Transactional
    public ResultData addMusicAlbum(MusicAlbum musicAlbum,String token) {
        musicAlbum.setId(StringUtil.getUUID());
        musicAlbum.setCreateTime(new Date());
        SysUser currentUser = sysUserService.getSysUserByToken(token).getData();
        musicAlbum.setCreateUserId(currentUser.getId());
        musicAlbum.setDel(StringConstant.DEL_NO);
        log.info("添加专辑:"+ JSON.toJSONString(musicAlbum));
        int i = musicAlbumMapper.addMusicAlbum(musicAlbum);
        if(i>0){
            return ResultData.createSuccess(null);
        }else{
            return ResultData.createFail("添加专辑失败");
        }
    }


    /**
     * 修改专辑
     *
     * @param musicAlbum
     * @return
     */
    @Transactional
    public ResultData updateMusicAlbum(MusicAlbum musicAlbum,String token) {
        if(StringUtils.isEmpty(musicAlbum.getId())){
            return ResultData.createFail("id不能为空");
        }
        SysUser currentUser = sysUserService.getSysUserByToken(token).getData();
        musicAlbum.setUpdateUserId(currentUser.getId());
        musicAlbum.setUpdateTime(new Date());
        log.info("修改专辑:"+ JSON.toJSONString(musicAlbum));
        int i = musicAlbumMapper.updateMusicAlbum(musicAlbum);
        if(i>0){
            return ResultData.createSuccess(null);
        }else{
            return ResultData.createFail("修改专辑失败");
        }
    }


    /**
     * 删除专辑
     *
     * @param musicAlbum
     * @return
     */
    @Transactional
    public ResultData delMusicAlbum(MusicAlbum musicAlbum) {
        if(StringUtils.isEmpty(musicAlbum.getId())){
            return ResultData.createFail("id不能为空");
        }
        musicAlbum.setDel(StringConstant.DEL_OK);
        log.info("删除专辑:"+ JSON.toJSONString(musicAlbum));
        int i = musicAlbumMapper.deleteMusicAlbum(musicAlbum);
        if(i>0){
            return ResultData.createSuccess(null);
        }else{
            return ResultData.createFail("删除专辑失败");
        }
    }
}
